//
//  DataViewController.m
//  Sky
//
//  Created by chen on 16/6/28.
//  Copyright (c) 2016年 chen. All rights reserved.
//

#import "DataViewController.h"
#import "ClockView.h"
#import "BlurBackground.h"
#import "APIStoreSDK.h"
#import "AirDetail.h"
#import "TempLineViewController.h"

@interface DataViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic) UILabel *cityName;

@property (nonatomic) UILabel *temperature_current;
@property (nonatomic) UILabel *temperature_high;
@property (nonatomic) UILabel *temperature_low;

@property (nonatomic) UIButton *airButton;


@property (nonatomic) UILabel *weather_today;
@property (nonatomic) UILabel *weather_future1;
@property (nonatomic) UILabel *weather_future2;
@property (nonatomic) UILabel *weather_future3;
@property (nonatomic) UILabel *weather_future4;

@property (nonatomic) UILabel *temperature_future1;
@property (nonatomic) UILabel *temperature_future2;
@property (nonatomic) UILabel *temperature_future3;
@property (nonatomic) UILabel *temperature_future4;

@property (nonatomic) UIImageView *icon_today;
@property (nonatomic) UIImageView *icon_future1;
@property (nonatomic) UIImageView *icon_future2;
@property (nonatomic) UIImageView *icon_future3;
@property (nonatomic) UIImageView *icon_future4;

@property (nonatomic) UILabel *week_today;
@property (nonatomic) UILabel *week_future1;
@property (nonatomic) UILabel *week_future2;
@property (nonatomic) UILabel *week_future3;
@property (nonatomic) UILabel *week_future4;

@property (nonatomic) UIView *popView;

@property (nonatomic)ClockView *clockView;

@property (strong,nonatomic) NSMutableDictionary *dataDic;
@property (nonatomic)UITableView  *tableView;
@end

@implementation DataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    
    /**************************UI创建************************************/
    //导航栏和状态栏高度64
    static CGFloat missHeight=64;
    CGFloat height=self.view.frame.size.height-missHeight;
    CGFloat width=self.view.frame.size.width;
    [self createTopView:CGRectMake(0,0, width, height *0.44)];
    //先创建底部View
    [self createBottomView:CGRectMake(0, height*0.70, width, height*0.30)];
    //后创建中部View
    [self createMiddleView:CGRectMake(0, height*0.44, width, height*0.26)];
    
    self.dataDic=[[NSMutableDictionary alloc]init];
 
    
    [self fetchFromServer];

}

-(void)createTopView:(CGRect)rect
{
    self.clockView=[[ClockView alloc]initWithFrame:rect];
    self.clockView.backgroundColor=[UIColor whiteColor];
    [self.clockView setCurrentTemperature:10 withLowTemp:0 withHigh:40];
    [self.view addSubview:self.clockView];
}


-(void)createMiddleView:(CGRect)rect
{
    CGFloat width=rect.size.width;
    CGFloat height=rect.size.height;
    UIView *middleView=[[UIView alloc]initWithFrame:rect];
    
    //实时天气图标
    self.icon_today=[[UIImageView alloc]initWithFrame:CGRectMake(width/6-self.icon_future1.frame.size.width/2, height/2-self.icon_future1.frame.size.height/2, self.icon_future1.frame.size.width, self.icon_future1.frame.size.height)];
    [middleView addSubview:self.icon_today];
    //实时天气温度
    self.temperature_current=[[UILabel alloc]initWithFrame:CGRectMake(width*2/3, 0, width/3, height*2/3)];
   // self.temperature_current.text=@"30°";
    self.temperature_current.textAlignment=NSTextAlignmentCenter;
    self.temperature_current.tintColor=[UIColor blackColor];
    self.temperature_current.font=[UIFont systemFontOfSize:40 weight:0.1];
    [middleView addSubview:self.temperature_current];
    
    
    //温度曲线图
    UIButton *temperatureLineBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    temperatureLineBtn.frame = CGRectMake(width*2/3, height*2/3, width/3, height/3);
    [temperatureLineBtn setTitle:@"温度曲线" forState:UIControlStateNormal];
    [temperatureLineBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [temperatureLineBtn addTarget:self action:@selector(temperatureLineBtnPreddes) forControlEvents:UIControlEventTouchUpInside];
    [middleView addSubview:temperatureLineBtn];
    
    self.weather_today=[[UILabel alloc]initWithFrame:CGRectMake(width/3, 0, width/3, height*0.25)];
   // self.weather_today.text=@"雷阵雨";
    self.weather_today.textAlignment=NSTextAlignmentCenter;
    self.weather_today.tintColor=[UIColor blackColor];
    self.weather_today.font=[UIFont systemFontOfSize:10];
    [middleView addSubview:self.weather_today];
    
    self.cityName=[[UILabel alloc]initWithFrame:CGRectMake(width/3, height*0.25, width*0.333, height*0.25)];
   // self.cityName.text=@"东莞市";
    self.cityName.textAlignment=NSTextAlignmentCenter;
    self.cityName.textColor=[UIColor blueColor];
    self.cityName.font=[UIFont systemFontOfSize:13];
    [middleView addSubview:self.cityName];
    
    self.week_today=[[UILabel alloc]initWithFrame:CGRectMake(width/3, height*0.5,width*0.333, height*0.25)];
  //  self.week_today.text=@"周四";
    self.week_today.textAlignment=NSTextAlignmentCenter;
    self.week_today.tintColor=[UIColor blackColor];
    self.week_today.font=[UIFont systemFontOfSize:10];
    [middleView addSubview:self.week_today];
    
    self.airButton=[[UIButton alloc]initWithFrame:CGRectMake(width/3, height*0.75, width*0.333, height*0.25)];
    self.airButton.titleLabel.textAlignment=NSTextAlignmentCenter;
    [self.airButton setTitle:@"空气质量 优 >" forState:UIControlStateNormal];
    [self.airButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [self.airButton setFont:[UIFont systemFontOfSize:11]];
    [self.airButton addTarget:self action:@selector(airButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [middleView addSubview:self.airButton];
    
    [self.view addSubview:middleView];
    
}

-(void)createBottomView:(CGRect)rect
{
    //创建底部view
    UIView *bottomView=[[UIView alloc]initWithFrame:rect];
    [self.view addSubview:bottomView];
    
    //创建分割线
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(15, 0, rect.size.width-30, 1)];
    lineView.backgroundColor=[UIColor colorWithRed:37/255.0f green:175/255.0f blue:148/255.0f alpha:1.0f];
    [bottomView addSubview:lineView];
    
    //创建四个子块
    CGFloat space=5;
    CGFloat width=(bottomView.frame.size.width-space*5)/4;
    CGFloat height=bottomView.frame.size.height;
    UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(space, 0, width, height)];
    [bottomView addSubview:view1];
    UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(space*2+width, 0, width, height)];
    [bottomView addSubview:view2];
    UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(space*3+width*2, 0, width, height)];
    [bottomView addSubview:view3];
    UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(space*4+width*3, 0, width, height)];
    [bottomView addSubview:view4];
    
    
    //添加显示控件
    self.icon_future1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height*0.4)];
    [view1 addSubview:self.icon_future1];
    
    self.weather_future1=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.4, width,height*0.125)];
    //self.weather_future1.text=@"小雨";
    self.weather_future1.textAlignment=NSTextAlignmentCenter;
    self.weather_future1.tintColor=[UIColor blackColor];
    self.weather_future1.font=[UIFont systemFontOfSize:10];
    [view1 addSubview:self.weather_future1];
    
    self.temperature_future1=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.525, width,height*0.125)];
    //self.temperature_future1.text=@"23/30";
    self.temperature_future1.textAlignment=NSTextAlignmentCenter;
    self.temperature_future1.tintColor=[UIColor blackColor];
    self.temperature_future1.font=[UIFont systemFontOfSize:10];
    [view1 addSubview:self.temperature_future1];
    
    self.week_future1=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.65, width,height*0.35)];
    //self.week_future1.text=@"周三";
    self.week_future1.textAlignment=NSTextAlignmentCenter;
    self.week_future1.tintColor=[UIColor blackColor];
    self.week_future1.font=[UIFont systemFontOfSize:12];
    [view1 addSubview:self.week_future1];
    
    self.icon_future2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height*0.4)];
    [view2 addSubview:self.icon_future2];
    
    self.weather_future2=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.4, width,height*0.125)];
    //self.weather_future2.text=@"小雨";
    self.weather_future2.textAlignment=NSTextAlignmentCenter;
    self.weather_future2.tintColor=[UIColor blackColor];
    self.weather_future2.font=[UIFont systemFontOfSize:10];
    [view2 addSubview:self.weather_future2];
    
    self.temperature_future2=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.525, width,height*0.125)];
    //self.weather_future2.text=@"23/30";
    self.temperature_future2.textAlignment=NSTextAlignmentCenter;
    self.temperature_future2.tintColor=[UIColor blackColor];
    self.temperature_future2.font=[UIFont systemFontOfSize:10];
    [view2 addSubview:self.temperature_future2];
    
    self.week_future2=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.65, width,height*0.35)];
    //self.weather_future2.text=@"周三";
    self.week_future2.textAlignment=NSTextAlignmentCenter;
    self.week_future2.tintColor=[UIColor blackColor];
    self.week_future2.font=[UIFont systemFontOfSize:10];
    [view2 addSubview:self.week_future2];
    
    self.icon_future3=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height*0.4)];
    [view3 addSubview:self.icon_future3];
    
    self.weather_future3=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.4, width,height*0.125)];
    //self.weather_future3.text=@"小雨";
    self.weather_future3.textAlignment=NSTextAlignmentCenter;
    self.weather_future3.tintColor=[UIColor blackColor];
    self.weather_future3.font=[UIFont systemFontOfSize:10];
    [view3 addSubview:self.weather_future3];
    
    self.temperature_future3=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.525, width,height*0.125)];
    //self.weather_future3.text=@"23/30";
    self.temperature_future3.textAlignment=NSTextAlignmentCenter;
    self.temperature_future3.tintColor=[UIColor blackColor];
    self.temperature_future3.font=[UIFont systemFontOfSize:10];
    [view3 addSubview:self.temperature_future3];
    
    self.week_future3=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.65, width,height*0.35)];
    //self.weather_future3.text=@"周三";
    self.week_future3.textAlignment=NSTextAlignmentCenter;
    self.week_future3.tintColor=[UIColor blackColor];
    self.week_future3.font=[UIFont systemFontOfSize:12];
    [view3 addSubview:self.week_future3];
    
    self.icon_future4=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height*0.4)];
    [view4 addSubview:self.icon_future4];
    
    self.weather_future4=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.4, width,height*0.125)];
    //self.weather_future4.text=@"小雨";
    self.weather_future4.textAlignment=NSTextAlignmentCenter;
    self.weather_future4.tintColor=[UIColor blackColor];
    self.weather_future4.font=[UIFont systemFontOfSize:10];
    [view4 addSubview:self.weather_future4];
    
    self.temperature_future4=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.525, width,height*0.125)];
    //self.temperature_future4.text=@"23/30";
    self.temperature_future4.textAlignment=NSTextAlignmentCenter;
    self.temperature_future4.tintColor=[UIColor blackColor];
    self.temperature_future4.font=[UIFont systemFontOfSize:10];
    [view4 addSubview:self.temperature_future4];
    
    self.week_future4=[[UILabel alloc]initWithFrame:CGRectMake(0,height*0.65, width,height*0.35)];
    self.week_future4.textAlignment=NSTextAlignmentCenter;
    self.week_future4.tintColor=[UIColor blackColor];
    self.week_future4.font=[UIFont systemFontOfSize:10];
    [view4 addSubview:self.week_future4];
}

#pragma mark - button function

-(void)airButtonPressed
{
    
    [self PopView];
}

-(void)closeButtonPressed
{
    [self.popView removeFromSuperview];
    // [[BlurBackground sharedBlurground]removeBackgroundFromView];
}

-(void)temperatureLineBtnPreddes{
    NSMutableArray *XArray = [[ NSMutableArray alloc]init];
    NSMutableArray *YArrayLow = [[ NSMutableArray alloc]init];
    NSMutableArray *YArrayHigh = [[ NSMutableArray alloc]init];

    NSDictionary *dic=[NSDictionary dictionaryWithDictionary:[[self.dataDic objectForKey:@"HeWeather data service 3.0"]objectAtIndex:0]];
    
    for (int i=0; i<7; i++) {
        NSString *week = [self featureWeekdayWithDate:[[[dic objectForKey:@"daily_forecast"]objectAtIndex:i]objectForKey:@"date"]];
        [XArray addObject:week];
    }
    NSLog(@"XArray");

    
    for (int i=0; i<7; i++) {
        NSString *temp = [[[[dic objectForKey:@"daily_forecast"]objectAtIndex:i]objectForKey:@"tmp"]objectForKey:@"min"];
        [YArrayLow addObject:temp];
    }
    NSLog(@"YArrayLow");
    
    for (int i=0; i<7; i++) {
        NSString *temp = [[[[dic objectForKey:@"daily_forecast"]objectAtIndex:i]objectForKey:@"tmp"]objectForKey:@"max"];
        [YArrayHigh addObject:temp];
    }
    NSLog(@"XArrayHigh");
    
    TempLineViewController *viewController = [[TempLineViewController alloc]init];
    viewController.XArray = XArray;
    viewController.YArrayLow = YArrayLow;
    viewController.YArrayHigh = YArrayHigh;
    [self presentViewController:viewController animated:YES completion:nil];
}


#pragma mark - popView

-(void)PopView
{

#pragma mark -popView和tableView重读初始化
    
    self.popView=[[UIView alloc]initWithFrame:CGRectMake(20, 80, self.view.frame.size.width-40, self.view.frame.size.height-80)];
    self.popView.backgroundColor=[UIColor colorWithRed:37/255.0f green:175/255.0f blue:148/255.0f alpha:0.96f];
    UIButton *closeButton=[[UIButton alloc]initWithFrame:CGRectMake(0,self.popView.frame.size.height-30,self.popView.frame.size.width,30)];
    closeButton.titleLabel.textAlignment=NSTextAlignmentCenter;
    [closeButton setTitle:@"确认" forState:UIControlStateNormal];
    [closeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [closeButton setFont:[UIFont systemFontOfSize:12]];
    [closeButton addTarget:self action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(5, 5, self.popView.frame.size.width-10, self.popView.frame.size.height-35) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerClass:[AirDetail class] forCellReuseIdentifier:@"CellIdentifier"];
    [self.popView addSubview:self.tableView];
    
    
    [self.popView addSubview:closeButton];
    
    
    [[UIApplication sharedApplication].windows.lastObject addSubview:self.popView];

    
}

#pragma mark -tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic=[[NSDictionary dictionaryWithDictionary:[[self.dataDic objectForKey:@"HeWeather data service 3.0"]objectAtIndex:0]]objectForKey:@"suggestion"];
    NSArray *values=[NSArray arrayWithArray:[dic allValues]];
    return [values count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AirDetail *cell=[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    if (cell==nil) {
        cell=[[AirDetail alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
    }
    
    NSDictionary *dic=[[NSDictionary dictionaryWithDictionary:[[self.dataDic objectForKey:@"HeWeather data service 3.0"]objectAtIndex:0]]objectForKey:@"suggestion"];
    NSArray *values=[NSArray arrayWithArray:[dic allValues]];
    cell.label.font = [UIFont systemFontOfSize:13];
    cell.label.text=[[values objectAtIndex:indexPath.row]objectForKey:@"txt"];
    
    return cell;
}




-(void)fetchFromServer
{
    /**************************拉取数据************************************/

    NSString *apikey=@"d18d6dc4a390434b38d10f470bf3c030";
    NSString *uri = @"http://apis.baidu.com/heweather/weather/free";
    NSMutableDictionary *parameter=[NSMutableDictionary dictionaryWithObject:self.cityString forKey:@"city"];
    APISCallBack *callBack=[APISCallBack alloc];
    callBack.onSuccess=^(long states,NSString *responseString){
        NSLog(@"onSuccess");
        if (responseString!=nil) {
            NSData *jsonData=[responseString dataUsingEncoding:NSUTF8StringEncoding];
            self.dataDic=[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
            NSString *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
            NSString *filePath=[path stringByAppendingPathComponent:[self.cityString stringByAppendingString:@".plist"]];
            [self.dataDic writeToFile:filePath atomically:YES];
            [self refresh];

        }else{
            NSString *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
            NSString *filePath=[path stringByAppendingPathComponent:[self.cityString stringByAppendingString:@".plist"]];
            NSFileManager *filemange=[NSFileManager defaultManager];
            if ([filemange fileExistsAtPath:filePath]) {
                self.dataDic=[NSMutableDictionary dictionaryWithContentsOfFile:filePath];
                [self refresh];
            };
        }
    };
    
    callBack.onError=^(long states,NSString *responseString){
        NSString *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        NSString *filePath=[path stringByAppendingPathComponent:[self.cityString stringByAppendingString:@".plist"]];
        NSFileManager *filemange=[NSFileManager defaultManager];
        if ([filemange fileExistsAtPath:filePath]) {
            self.dataDic=[NSMutableDictionary dictionaryWithContentsOfFile:filePath];
            [self refresh];
        };
    };
    
    callBack.onComplete=^(){
        NSLog(@"onComplete");
    };
    
    [ApiStoreSDK executeWithURL:uri method:@"post" apikey:apikey parameter:parameter callBack:callBack];
    
    
    
}

-(void)refresh
{
    NSDictionary *dic=[NSDictionary dictionaryWithDictionary:[[self.dataDic objectForKey:@"HeWeather data service 3.0"]objectAtIndex:0]];
    
    self.weather_today.text=[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:0]objectForKey:@"cond"]objectForKey:@"txt_d"];
    self.weather_future1.text=[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"cond"]objectForKey:@"txt_d"];
    self.weather_future2.text=[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:2]objectForKey:@"cond"]objectForKey:@"txt_d"];
    self.weather_future3.text=[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:3]objectForKey:@"cond"]objectForKey:@"txt_d"];
    self.weather_future4.text=[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:4]objectForKey:@"cond"]objectForKey:@"txt_d"];
    
    self.icon_today.image=[UIImage imageNamed:[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:0]objectForKey:@"cond"]objectForKey:@"code_d"] stringByAppendingString:@".png"]];
    self.icon_future1.image=[UIImage imageNamed:[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"cond"]objectForKey:@"code_d"] stringByAppendingString:@".png"]];
    self.icon_future2.image=[UIImage imageNamed:[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:2]objectForKey:@"cond"]objectForKey:@"code_d"] stringByAppendingString:@".png"]];
    self.icon_future3.image=[UIImage imageNamed:[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:3]objectForKey:@"cond"]objectForKey:@"code_d"] stringByAppendingString:@".png"]];
    self.icon_future4.image=[UIImage imageNamed:[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:4]objectForKey:@"cond"]objectForKey:@"code_d"] stringByAppendingString:@".png"]];
    

    self.temperature_future1.text=[[[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"tmp"]objectForKey:@"min"] stringByAppendingString:@"°/"] stringByAppendingString:[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"tmp"]objectForKey:@"max"]] stringByAppendingString:@"°"];
    self.temperature_future2.text=[[[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:2]objectForKey:@"tmp"]objectForKey:@"min"] stringByAppendingString:@"°/"] stringByAppendingString:[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"tmp"]objectForKey:@"max"]] stringByAppendingString:@"°"];
    self.temperature_future3.text=[[[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:3]objectForKey:@"tmp"]objectForKey:@"min"] stringByAppendingString:@"°/"] stringByAppendingString:[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"tmp"]objectForKey:@"max"]] stringByAppendingString:@"°"];
    self.temperature_future4.text=[[[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:4]objectForKey:@"tmp"]objectForKey:@"min"] stringByAppendingString:@"°/"] stringByAppendingString:[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"tmp"]objectForKey:@"max"]] stringByAppendingString:@"°"];
    
    self.week_today.text=[self featureWeekdayWithDate:[[[dic objectForKey:@"daily_forecast"]objectAtIndex:0]objectForKey:@"date"]];
    self.week_future1.text=[self featureWeekdayWithDate:[[[dic objectForKey:@"daily_forecast"]objectAtIndex:1]objectForKey:@"date"]];
    self.week_future2.text=[self featureWeekdayWithDate:[[[dic objectForKey:@"daily_forecast"]objectAtIndex:2]objectForKey:@"date"]];
    self.week_future3.text=[self featureWeekdayWithDate:[[[dic objectForKey:@"daily_forecast"]objectAtIndex:3]objectForKey:@"date"]];
    self.week_future4.text=[self featureWeekdayWithDate:[[[dic objectForKey:@"daily_forecast"]objectAtIndex:4]objectForKey:@"date"]];
    
    int currentTempLow=[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:0]objectForKey:@"tmp"]objectForKey:@"min"]intValue];  //NSString转int
    int currentTempHigh=[[[[[dic objectForKey:@"daily_forecast"]objectAtIndex:0]objectForKey:@"tmp"]objectForKey:@"max"]intValue];  //NSString转int
    int currentTmp =[[[dic objectForKey:@"now"]objectForKey:@"tmp"]intValue];
    
    self.temperature_current.text=[[[dic objectForKey:@"now"]objectForKey:@"tmp"]stringByAppendingString:@"°"];
    
    [self.clockView setCurrentTemperature:currentTmp withLowTemp:currentTempLow withHigh:currentTempHigh];
    
    self.cityName.text=[[dic objectForKey:@"basic"]objectForKey:@"city"]; //显示城市
    NSString *airquality=[[[[dic objectForKey:@"aqi"]objectForKey:@"city"]objectForKey:@"qlty" ]stringByAppendingString:@" 更多>"];
    [self.airButton setTitle:airquality forState:UIControlStateNormal];
    
}


/**
 *  获取未来某个日期是星期几
 *  注意：featureDate 传递过来的格式 必须 和 formatter.dateFormat 一致，否则endDate可能为nil
 *
 */
- (NSString *)featureWeekdayWithDate:(NSString *)featureDate{
    NSLog(@"日期转换：%@",featureDate);
    // 创建 格式 对象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置 日期 格式 可以根据自己的需求 随时调整， 否则计算的结果可能为 nil
    formatter.dateFormat = @"yyyy-MM-dd";
    // 将字符串日期 转换为 NSDate 类型
    NSDate *endDate = [formatter dateFromString:featureDate];
    // 判断当前日期 和 未来某个时刻日期 相差的天数
    long days = [self daysFromDate:[NSDate date] toDate:endDate];
    // 将总天数 换算为 以 周 计算（假如 相差10天，其实就是等于 相差 1周零3天，只需要取3天，更加方便计算）
    long day = days >= 7 ? days % 7 : days;
    long week = [self getNowWeekday] + day;
    week = week > 7 ? week %7: week;
    switch (week) {
        case 1:
            return @"周日";
            break;
        case 2:
            return @"周一";
            break;
        case 3:
            return @"周二";
            break;
        case 4:
            return @"周三";
            break;
        case 5:
            return @"周四";
            break;
        case 6:
            return @"周五";
            break;
        case 7:
            return @"周六";
            break;
            
        default:
            return @"错误";
            break;
    }
    return nil;
}

/**
 *  计算2个日期相差天数
 *  startDate   起始日期
 *  endDate     截至日期
 */
-(NSInteger)daysFromDate:(NSDate *)startDate toDate:(NSDate *)endDate {
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    // 话说在真机上需要设置区域，才能正确获取本地日期，天朝代码:zh_CN
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //得到相差秒数
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    int days = ((int)time)/(3600*24);
    int hours = ((int)time)%(3600*24)/3600;
    int minute = ((int)time)%(3600*24)/3600/60;
    if (days <= 0 && hours <= 0&&minute<= 0) {
        NSLog(@"0天0小时0分钟");
        return 0;
    }
    else {
        NSLog(@"%@",[[NSString alloc] initWithFormat:@"%i天%i小时%i分钟",days,hours,minute]);
        // 之所以要 + 1，是因为 此处的days 计算的结果 不包含当天 和 最后一天\
        （如星期一 和 星期四，计算机 算的结果就是2天（星期二和星期三），日常算，星期一——星期四相差3天，所以需要+1）\
        对于时分 没有进行计算 可以忽略不计
        return days + 1;
    }
}

// 获取当前是星期几
- (NSInteger)getNowWeekday {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDate *now = [NSDate date];
    // 话说在真机上需要设置区域，才能正确获取本地日期，天朝代码:zh_CN
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:now];
    return [comps weekday];
}

@end
